package cn.trigram.web;

import cn.trigram.ddo.entity.Service01OrderEntity;
import cn.trigram.mapper.Service01OrderMapper;
import cn.trigram.mq.Service01MqProducer;
import com.apifan.common.random.source.PersonInfoSource;
import java.util.UUID;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@Setter(onMethod_ = {@Autowired})
@RestController
@RequestMapping("/mq")
public class MqController {

  private Service01MqProducer mqProducer;

  private Service01OrderMapper orderMapper;

  @GetMapping("/sync_send/{msgContent}")
  public String syncSend(
      @PathVariable("msgContent")
          String msgContent
  ) {

    String uuid = UUID.randomUUID().toString();
    mqProducer.syncSend("sync-send", uuid, uuid + ":" + msgContent);
    return "发送成功";
  }

  @GetMapping("/tx_send/{status}")
  public String txSend(
      @PathVariable("status")
          String status
  ) {

    String               uuid        = UUID.randomUUID().toString();
    Service01OrderEntity orderEntity = new Service01OrderEntity();
    orderEntity.setId(uuid);
    orderEntity.setName(PersonInfoSource.getInstance().randomChineseName());
    orderEntity.setStatus(status);
    orderEntity.setPayStatus("no_pay");
    orderMapper.insertSelective(orderEntity);
    mqProducer.transactionSend("transaction-send", uuid, orderEntity);
    return "发送成功";
  }

}
